import queue
import threading

lock = threading.Lock()

def worker(q):
    while True:
        item = q.get()
        with lock:
            print("Processing", item)
        q.task_done()

if __name__ == '__main__':
    q = queue.Queue()
    for i in range(5):
        q.put(i)

    for i in range(3):
        t = threading.Thread(target=worker, args=(q,))
        t.daemon = True
        t.start()

    q.join()
    print("All items processed.")